<?php
namespace app\home\controller;

use think\Controller;
use think\captcha\Captcha;

class Login extends Controller{

    public function __construct(){
        parent::__construct();
        $this->view->engine->layout('layout/loginLayout');
    }

    public function index(){
        if(session('?userName') && ((time() - session('userExpire')) < config('session.expire'))){
            $this->redirect('home/index/index');
        }
        return $this->fetch();
    }

    public function logIn(){
        if(request()->isAjax()){
            $username = input('username');
            $password = md5(input('password'));
            $ary = ['status'=>2];
            $result = db('user')->where(['user_name'=>$username,'password'=>$password])->find();
            if($result){
                session('userId',$result['id']);
                session('userName',$username);
                session('userExpire',time());
                $ary = ['status'=>1];
            }
            return $ary;
        }
    }

    public function Out(){
        session(null);
        $this->redirect('home/index/index');
    }

    public function verify(){
        $index = empty(input('index')) ? '':input('index');
        $captcha = new Captcha();
        $captcha->codeSet = '0123456789';
        $captcha->length   = 4;
        $captcha->fontSize   = 30;
        return $captcha->entry($index);
    }

}
